package com.seed.application.controller;

import cn.hutool.core.util.StrUtil;
import com.seed.application.Sa;
import com.seed.application.env.ActionEnv;
import com.seed.application.service.SeedService1;
import com.seed.application.service.pojo.SeedAction;
import com.seed.core.RequestWrapper;
import com.seed.core.ResponseWrapper;
import com.seed.core.action.Action;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.Micro;
import com.seed.util.SeedKit;
import com.seed.util.ThrowKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("${server.error.path:${error.path:/error}}")
public class SeedController implements ErrorController {

    private final RequestWrapper request;
    private final ResponseWrapper response;

    public SeedController(HttpServletRequest request, HttpServletResponse response) {
        this.request = new RequestWrapper(request);
        this.response = new ResponseWrapper(response);
    }

    @RequestMapping
    public void error() throws ServletException, IOException {
        HttpStatus status = request.getStatus();
        if(status.equals(HttpStatus.NOT_FOUND)) {
            String uri = request.getURI();
            ActionEnv.set(uri);
            ActionEnv.setMethod(request.getMethod());
            SeedAction seedAction = getSeedAction();
            Seed seed = seedAction.getSeed();
            Action action = seedAction.getAction();
            new SeedService1(seed, action, request, response).exec();
        } else {
            throw new RuntimeException("服务器出错了，请联系管理员");
        }
    }

    private SeedAction getSeedAction() {
        String seedName = ActionEnv.getSeedName();
        String actionName = ActionEnv.getActionName();
        RequestMethod method = ActionEnv.getMethod();
        Micro micro = Sa.getMicro();
        Seed seed;
        Action action;
        if(StrUtil.isBlank(seedName)) {
            Map<String, Action> actions = micro.getActions();
            if(actions == null) ThrowKit.exception("请求的方法【%s】不存在", actionName);

            action = actions.get(actionName);
            if(action == null || action.getMethods() == null || !(action.getMethods()).contains(method)) {
                ThrowKit.exception("请求的方法【%s】不存在", actionName);
            }

            seedName = action.getSeedName();
            if(StrUtil.isBlank(seedName)) ThrowKit.exception("【%s】必需配置【seedName】", actionName);
            seed = micro.getSeed(seedName);
        } else {
            seed = micro.getSeed(seedName);
            action = SeedKit.getAction(seed, method, actionName);
        }

        return new SeedAction(seed, action);
    }
}
